

package io.renren.modules.app.controller;


import io.renren.common.annotation.SysLog;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Example;
import io.swagger.annotations.ExampleProperty;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * APP测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api(tags = "APP用户接口")
public class AppUserController {

    @Autowired
    private UserService userService;

    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok().put("user", user);
    }

    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "所有用户列表", notes = "可以根据用户名、创建者ID、创建时间范围进行筛选")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "currentPage", value = "当前页码", paramType = "query",  dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", paramType = "query", dataType = "int")
    })
    @ApiResponses({
        @ApiResponse(code = 0, message = "成功", response = R.class),
        @ApiResponse(code = 500, message = "服务器错误", response = R.class)
    })
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        
        return R.ok().put("page", page);
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    @ApiOperation("删除用户")
    public R delete(@RequestBody Long[] userIds){
        userService.deleteBatch(userIds);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")
    @ApiOperation("修改用户")
    public R update(@RequestBody UserEntity user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        userService.update(user);
        return R.ok();
    }

}
